<template>
  <div class="card-view">
    <div class="header">
      <div class="title">
        <slot name="title">{{title}}</slot>
      </div>
      <div class="btns">
        <slot name="btns"></slot>
      </div>
    </div>
    <div class="params">
      <h4>params</h4>
      <textarea v-model="paramVal" cols="30" rows="10"></textarea>
    </div>
    <div class="body">
      <h4>body</h4>
      <textarea :value="JSON.stringify(body, null, 2)" readonly cols="30" rows="10"></textarea>
    </div>
  </div>
</template>

<script>
export default {
  name: 'CardView',
  props: {
    title: String,
    params: Object,
    body: [Object, String]
  },
  data() {
    return {
      paramVal: '',
    }
  },
  watch: {
    params: {
      handler(val) {
        this.paramVal = JSON.stringify(val, null, 2);
      },
      deep: true,
      immediate: true
    },
    paramVal(val) {
      try {
        this.$emit('update:params', JSON.parse(val))
      } catch (error) {
        console.warn(error);
      }
    }
  }
}
</script>

<style scoped>
.card-view {
  padding: 10px;
  margin: 20px 0;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-shadow: 1px 1px 10px 1px rgba(0,0,0,0.2);
}
.card-view .header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}
.card-view .header .title {
  font-size: 20px;
  font-weight: bold;
}
.card-view .params {
  margin-bottom: 10px;
}
.card-view textarea {
  width: 100% !important;
}
</style>